package com.huatec.service.impl;

import com.huatec.dao.StudentDao;
import com.huatec.dao.TeacherDao;
import com.huatec.entity.Student;
import com.huatec.entity.Teacher;
import com.huatec.service.UserService;
import com.huatec.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private StudentDao studentDao;
    @Override
    public Result checkLogin(int sign, String username, String password) {
        //sign=0,教师登录；sign=1，学生登录
        Result result = new Result();
        if(sign==0){
//            教师登录,根据用户名查询用户是否存在
            Teacher teacher = teacherDao.findByName(username);
            if(teacher==null){
                result.setStatus(1);
                result.setMsg("此用户不存在");
                return result;
            }
            if(!password.equals(teacher.getPassword())){
                result.setStatus(1);
                result.setMsg("密码错误");
                return result;
            }
            result.setStatus(0);
            result.setMsg("用户名和密码正确");
            result.setData(teacher);
        }else {
//            学生登录
            Student student = studentDao.findByName(username);
            if(student==null){
                result.setStatus(1);
                result.setMsg("此用户不存在");
                return result;
            }
            if(!password.equals(student.getPassword())){
                result.setStatus(1);
                result.setMsg("密码错误");
                return result;
            }
            result.setStatus(0);
            result.setMsg("用户名和密码正确");
            result.setData(student);
        }
        return result;
    }
}
